package cn.tedu.cqhotel.controller;

import cn.tedu.cqhotel.pojo.dto.StaffAddNewDTO;
import cn.tedu.cqhotel.pojo.dto.StaffUpdateDTO;
import cn.tedu.cqhotel.pojo.vo.StaffListItemVO;
import cn.tedu.cqhotel.pojo.vo.StaffStandardVO;
import cn.tedu.cqhotel.service.IStaffService;
import cn.tedu.cqhotel.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理员工相关请求的控制器
 *
 * @author Wangdongrui
 */
@Slf4j
@RestController
@RequestMapping("/staffs")
@Api(tags = "05. 员工模块")
@Validated
public class StaffController {

    @Autowired
    private IStaffService staffService;

    public StaffController() {
        log.info("创建控制器：StaffController");
    }

    @ApiOperation("添加员工")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Validated StaffAddNewDTO staffAddNewDTO) {
        log.debug("开始处理【添加员工】的请求：{}", staffAddNewDTO);
        staffService.addNew(staffAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除员工")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "员工id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理【删除员工】的请求：id={}", id);
        staffService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改员工信息")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "员工id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, StaffUpdateDTO staffUpdateDTO) {
        log.debug("开始处理【修改员工信息】的请求：id={}, staffUpdateDTO={}", id, staffUpdateDTO);
        staffService.updateById(id, staffUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("启用员工")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "员工id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@PathVariable Long id) {
        log.debug("开始处理【启用员工】的请求：id={}", id);
        staffService.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation("禁用员工")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "员工id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@PathVariable Long id) {
        log.debug("开始处理【禁用员工】的请求：id={}", id);
        staffService.setDisable(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id查询员工信息")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "员工id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<StaffStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询员工信息】的请求：id={}", id);
        StaffStandardVO staff = staffService.getStandardById(id);
        return JsonResult.ok(staff);
    }

    @ApiOperation("根据name查询员工信息")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "name", value = "员工name", required = true, dataType = "string")
    @GetMapping("/name")
    public JsonResult<List<StaffListItemVO>> listByName(String name) {
        log.debug("开始处理【根据name查询员工信息】的请求：name={}", name);
        List<StaffListItemVO> list = staffService.listByName(name);
        return JsonResult.ok(list);
    }

    @ApiOperation("查询员工列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult<List<StaffListItemVO>> list() {
        log.debug("开始处理【查询员工列表】的请求……");
        List<StaffListItemVO> list = staffService.list();
        return JsonResult.ok(list);
    }

}
